package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.DanYuankejiServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "dany")
public class DanYuankejiAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(DanYuankejiAdaptor.class);

	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		String result = DanYuankejiServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		log.info("bsm- 单元科技订单订购请求返回结果：["+result+"],该订单号为："+cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("resultCode");
	
		if ("00".equals(code)) {
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
			orderBean.setSuppOrderNo(String.valueOf(json.get("orderId")));//需要上游的订单号
			
		} else {
		
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("resultMsg"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public void status() {
		
		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getSuppOrderNo();
		log.info("bsm- 单元科技订单查询请求订单号为："+cstmOrderNo);
	    if ("".equals(cstmOrderNo)) {
	    	orderBean.setMessage("该订单业务流水为空");
			orderBean.setErrorMsg("受理中，无订单");
			orderBean.setStatus("");
		}else {
			
		String result = DanYuankejiServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code =  json.getString("resultCode");

		
		if ("00".equals(code)) {
		
			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
			orderBean.setMessage(json.getString("resultMsg"));
			
		} else if (("02".equals(code))) {		
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("1");
			orderBean.setMessage(json.getString("resultMsg"));
		} else {
			orderBean.setErrorMsg(json.getString("resultMsg"));
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("resultMsg"));
		}
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
